package service

import (
	"context"
	"errors"
	"proto-server/basic/config"
	__ "proto-server/basic/proto"
	"proto-server/handler/model"
)

type Server struct {
	__.UnimplementedStreamGreeterServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Add(_ context.Context, in *__.AddReq) (*__.AddResp, error) {

	var findUser model.User
	
	if err := findUser.UserFind(config.DB, in.Phone); err != nil {
		return nil, err
	}

	if findUser.ID != 0 {
		return nil, errors.New("用户重复,请重新输入")
	}

	user := model.User{
		Phone:    in.Phone,
		Password: in.Password,
	}

	if err := user.UserAdd(config.DB); err != nil {
		return nil, err
	}

	return &__.AddResp{
		UserId: int64(user.ID),
	}, nil
}
